﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblCustTypeDAL:ImtblCustType
    {
        HKEA_CMSEntities model;
        mtblCustType type;
        public mtblCustTypeDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblCustTypeDAL GetInstance()
        {
            return new mtblCustTypeDAL();
        }

        public int InsertENT(mtblCustType ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblCustTypes.AddObject(ent);
            return model.SaveChanges();
        }

        /// <summary>
        /// 根据表的id获取一条记录
        /// </summary>
        /// <param name="uid">表的uid</param>
        /// <returns>一个实体（一条记录）</returns>
        public mtblCustType GetEntity(int uid)
        {
            return model.mtblCustTypes.Where(a=>a.CustTypeUID==uid).SingleOrDefault();
        }

        /// <summary>
        /// 根据实体组合参数获取一条记录
        /// </summary>
        /// <param name="ent">实体参数</param>
        /// <returns>一个实体（一条记录）</returns>
        public mtblCustType GetEntity(mtblCustType ent)
        {
            var result = model.mtblCustTypes.DefaultIfEmpty();
            if (ent.CustTypeUID > 0)
                result = result.Where(a => a.CustTypeUID == ent.CustTypeUID);
            if (ent.CustTypeUID > 0)
                result = result.Where(a => a.CustTypeUID == ent.CustTypeUID);
            if (ent.CustTypeUID > 0)
                result = result.Where(a => a.CustTypeUID == ent.CustTypeUID);
            if (ent.CustTypeUID > 0)
                result = result.Where(a => a.CustTypeUID == ent.CustTypeUID);
            return result.SingleOrDefault();
        }

        public List<mtblCustType> GetListByENT(mtblCustType ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblCustTypes.Where(a => a.CustTypeUID > 0);
            if (ent.CustTypeUID > 0)
                result = result.Where(a => a.CustTypeUID == ent.CustTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblCustType> GetListByENT(mtblCustType ent)
        {
            var result = model.mtblCustTypes.Where(a => a.CustTypeUID > 0);
            if (ent.CustTypeUID > 0)
                result = result.Where(a => a.CustTypeUID == ent.CustTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblCustType ent)
        {
            var result = model.mtblCustTypes.Where(a => a.CustTypeUID > 0);
            if (ent.CustTypeUID > 0)
                result = result.Where(a => a.CustTypeUID == ent.CustTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblCustType ent)
        {
            type = model.mtblCustTypes.Where(a=>a.CustTypeUID==ent.CustTypeUID).FirstOrDefault();
            if (type != null)
            {
                type.CustType_Chi = ent.CustType_Chi;
                type.CustType_Eng = ent.CustType_Eng;
                type.Deleted = ent.Deleted;
                type.LastModifyBy = ent.LastModifyBy;
                type.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblCustType ent)
        {
            type = model.mtblCustTypes.Where(a => a.CustTypeUID == ent.CustTypeUID).FirstOrDefault();
            if (type != null)
            {
                model.mtblCustTypes.DeleteObject(type);
            }
            return model.SaveChanges();
        }
    }
}
